package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.NoSuchElementException;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class MuriEPareti extends JPanel {

	private JTextField yearCostr = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField year1mod = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField year2mod = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private JTextField quadratura = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private String[] elFondazioni = { " ", "Platea in C.A.",
			"Continua in C.A.", "non presente", "muro misto", "mattoni",
			"pietra", "altro" };

	private JComboBox fondazioni = new JComboBox(elFondazioni);
	private boolean modifyFondazioni = false;
	private JTextField otherFondazioni = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private StartingWindow st;

	public MuriEPareti(StartingWindow st) {
		super();
		setOpaque(false);
		this.st = st;
		setLayout(new MigLayout());

		RoundedPanel yearCostruzioneLabel = new RoundedPanel(
				"Anno fine costruzione : ");
		setCell(yearCostruzioneLabel, 35);
		RoundedPanel yearModifica1 = new RoundedPanel("Anno ampliamento : ");
		setCell(yearModifica1, 35);
		RoundedPanel fondazioniLabel = new RoundedPanel("Fondazioni");
		setCell(fondazioniLabel, 45);
		RoundedPanel metriQ = new RoundedPanel("Superficie lorda in pianta");
		setCell(metriQ, 45);

		setCell();
		altroActionListener();
		addMouse();

		add(yearCostruzioneLabel, "cell 0 0");
		add(yearCostr, "cell 1 0");
		add(yearModifica1, "cell 0 1");
		add(year1mod, "cell 1 1 ");
		add(year2mod, "cell 2 1");
		add(metriQ, "cell 0 2");
		add(quadratura, "cell 1 2");
		add(fondazioniLabel, "cell 0 3");
		add(fondazioni, "cell 1 3 2 1");
	}

	private void setCell(JComponent c, int h) {
		c.setFont(new Font("Arial", Font.PLAIN, 16));
		c.setPreferredSize(new Dimension(210, h));
		c.setOpaque(false);
	}

	public void addMouse() {
		MouseAdapter mlA1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(otherFondazioni);
					fondazioni.setSelectedIndex(0);
					add(fondazioni, "cell 1 3 2 1");
					modifyFondazioni = false;

					paintAll(getGraphics());
				}
			}
		};
		otherFondazioni.addMouseListener(mlA1);

	}

	private void setCell() {
		yearCostr.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		year1mod.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		year2mod.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		otherFondazioni
				.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		quadratura.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		yearCostr.setOpaque(false);
		year1mod.setOpaque(false);
		year2mod.setOpaque(false);
		otherFondazioni.setOpaque(false);
		quadratura.setOpaque(false);

		yearCostr.setFont(new Font("Arial", Font.PLAIN, 16));
		year1mod.setFont(new Font("Arial", Font.PLAIN, 16));
		year2mod.setFont(new Font("Arial", Font.PLAIN, 16));
		fondazioni.setFont(new Font("Arial", Font.PLAIN, 16));
		otherFondazioni.setFont(new Font("Arial", Font.PLAIN, 16));
		quadratura.setFont(new Font("Arial", Font.PLAIN, 16));

		yearCostr.setPreferredSize(new Dimension(90, 35));
		year1mod.setPreferredSize(new Dimension(90, 35));
		year2mod.setPreferredSize(new Dimension(90, 35));
		fondazioni.setPreferredSize(new Dimension(235, 45));
		otherFondazioni.setPreferredSize(new Dimension(235, 45));
		quadratura.setPreferredSize(new Dimension(90, 35));
	}

	private void altroActionListener() {
		fondazioni.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) fondazioni.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(fondazioni);
					add(otherFondazioni, "cell 1 3 2 1");
					modifyFondazioni = true;
					paintAll(getGraphics());
				}
			}
		});

	}

	public String print() {
		String print = "";
		print = print + "\n#" + yearCostr.getText();
		print = print + "\n#" + year1mod.getText();
		print = print + "\n#" + year2mod.getText();
		print = print + "\n#" + quadratura.getText();
		if (modifyFondazioni)
			print = print + "\n#" + otherFondazioni.getText();
		else
			print = print + "\n#" + fondazioni.getSelectedIndex();

		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		c.nextLine();
		yearCostr.setText(c.nextLine());
		year1mod.setText(c.nextLine());
		year2mod.setText(c.nextLine());
		quadratura.setText(c.nextLine());
		String tmp = c.nextLine();
		try {
			fondazioni.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			otherFondazioni.setText(tmp);
			remove(fondazioni);
			add(otherFondazioni, "cell 1 2 2 1");
			modifyFondazioni = true;
		}

	}

	public Document PDF(Document doc) {
		Paragraph dati;
		Chunk underline = new Chunk("3. Dati dell'immobile",
				FontFactory.getFont(FontFactory.TIMES, 16, Font.BOLD));
		underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

		try {

			doc.add(underline);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		dati = new Paragraph("3.1 Caratteristiche dei muri",
				FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD));
		try {
			dati.setSpacingAfter(10);
			dati.setSpacingBefore(10);
			doc.add(dati);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		PdfPTable table = new PdfPTable(6);
		int[] size = { 60, 15, 60, 15, 60, 15 };
		try {
			table.setWidths(size);
			table.setWidthPercentage(100);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
			table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
			table.getDefaultCell().setFixedHeight(70);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Phrase testo = new Phrase("Anno fine costruzione: ",
				FontFactory.getFont(FontFactory.TIMES, 10, Font.BOLD));
		PdfPCell cos = new PdfPCell(testo);
		cos.disableBorderSide(PdfPCell.RIGHT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		cos = new PdfPCell(new Phrase(yearCostr.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11)));
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		cos = new PdfPCell(new Phrase("Ampliamento/modifica: ",
				FontFactory.getFont(FontFactory.TIMES, 10, Font.BOLD)));
		cos.disableBorderSide(PdfPCell.RIGHT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);
		
		
		cos = new PdfPCell(new Phrase(year1mod.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11)));
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		cos = new PdfPCell(new Phrase("Ampliamento/modifica: ",
				FontFactory.getFont(FontFactory.TIMES, 10, Font.BOLD)));
		cos.disableBorderSide(PdfPCell.RIGHT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		cos = new PdfPCell(new Phrase(year2mod.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11)));
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);
		
		

		try {

			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		

		//
		table = new PdfPTable(3);
		int[] width = { 60, 60, 60 };
		try {
			table.setWidths(width);
			table.setWidthPercentage(100);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
			table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
			table.getDefaultCell().setFixedHeight(70);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// fondazioni
		testo = new Phrase("Fondazioni ", FontFactory.getFont(
				FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		cos = new PdfPCell(new Phrase((String) fondazioni.getSelectedItem(),
				FontFactory.getFont(FontFactory.TIMES, 11)));
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_LEFT);
		cos.setColspan(2);
		cos.setFixedHeight(20);
		table.addCell(cos);
		
		//superficie lorda
		testo = new Phrase("Superficie lorda in pianta ", FontFactory.getFont(
				FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		cos = new PdfPCell(new Phrase(quadratura.getText(),
				FontFactory.getFont(FontFactory.TIMES, 11)));
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_LEFT);
		cos.setColspan(2);
		cos.setFixedHeight(20);
		table.addCell(cos);
		
		try {

			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return doc;
	}
}
